/*

INET.H

Header containing InternetGetFile and a wrapper function, InternetDownloadFile which also makes the Internet connection.

*/

#ifndef INET_H
#define INET_H
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include <stdio.h>

#include <wininet.h>
#pragma comment (lib, "wininet.lib")

#include <mmsystem.h>
#pragma   comment(lib,"winmm.lib")   

#define   INTERNET_CONNECTION_OFFLINE                   0x20   

enum
{
	INTERNET_ERROR_OPENURL=1,
	INTERNET_ERROR_FILEOPEN,
	INTERNET_ERROR_READFILE,
	INTERNET_ERROR_OPEN
};

UINT InternetGetFile (HINTERNET IN hOpen, // Handle from InternetOpen()
                 CHAR *szUrl,        // Full URL
                 CHAR *szFileName,
				 HWND hwndProgress,
				 int idStatusText,
				 int idProgressBar)
{
       DWORD dwSize;
       CHAR   szHead[] = "Accept: */*\r\n\r\n";
       VOID* szTemp[16384];
       HINTERNET  hConnect;
       FILE * pFile;

       if ( !(hConnect = InternetOpenUrlA ( hOpen, szUrl, szHead,
             lstrlenA (szHead), INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_RELOAD, 0)))
       {
           return INTERNET_ERROR_OPENURL;
       }

       if  ( !(pFile = fopen (szFileName, "wb" ) ) )
      {
          return INTERNET_ERROR_FILEOPEN;
      }

	DWORD dwByteToRead = 0;
	DWORD dwSizeOfRq = 4;
	DWORD dwBytes = 0;

        if (!HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, 
                      (LPVOID)&dwByteToRead, &dwSizeOfRq, NULL))
	    {
		dwByteToRead = 0;
	}
	DWORD start;
	DWORD end;
	DWORD time;
	time = 10;
	start = timeGetTime();
       do
       {
          // Keep coping in 16 KB chunks, while file has any data left.
          // Note: bigger buffer will greatly improve performance.
          if (!InternetReadFile (hConnect, szTemp, 16384,  &dwSize) )
          {
              fclose (pFile);
            return INTERNET_ERROR_READFILE;
          }
          if (!dwSize)
              break;  // Condition of dwSize=0 indicate EOF. Stop.
          else
             fwrite(szTemp, sizeof (char), dwSize , pFile);
	dwBytes+=dwSize;
	if(dwByteToRead && hwndProgress)
	{
	SendDlgItemMessageA(hwndProgress, idProgressBar, WM_USER+2, (dwBytes*100)/dwByteToRead, 0);
	UpdateWindow(hwndProgress);
	}
	FLOAT fSpeed = 0;
	fSpeed = (float)dwBytes;
	fSpeed /= ((float)time)/1000.0f;
	fSpeed /= 1024.0f;
	if(hwndProgress)
	{
		char s[260];
		sprintf(s, "%d KB / %d KB @ %1.1f KB/s", dwBytes/1024, dwByteToRead/1024, fSpeed);
		SetDlgItemTextA(hwndProgress, idStatusText, s);
		UpdateWindow(hwndProgress);
	}
	end = timeGetTime();
	time = end - start;
	if(time == 0)
	time = 10;
       }   // do
      while (TRUE);
      fflush (pFile);
      fclose (pFile);
      return 0;
}

int InternetDownloadFile(char *URL, char *FileDest, HWND progressWindow=NULL, int idStatusText=0, int idProgressBar=0)
{
DWORD dwFlags;
DWORD dwResult = INTERNET_ERROR_OPEN;
InternetGetConnectedState(&dwFlags, 0);
if(dwFlags & INTERNET_CONNECTION_OFFLINE)//take appropriate steps
	return INTERNET_ERROR_OPEN;
CHAR strAgent[64];
sprintf(strAgent, "Agent%ld", timeGetTime());
HINTERNET hOpen;
if(!(dwFlags & INTERNET_CONNECTION_PROXY))
hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
else
hOpen = InternetOpenA(strAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if(hOpen)
{
	dwResult = InternetGetFile(hOpen, URL, FileDest, progressWindow, idStatusText, idProgressBar);
	InternetCloseHandle(hOpen);
}
else return INTERNET_ERROR_OPEN;
return dwResult;
}

#endif /*INET_H*/